/* 
* @Author: Administrator
* @Date:   2014-04-02 21:05:48
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-19 17:26:48
*/

$(document).ready(function(){
	//HDJS自动验证收货人地址
	$('#new_address_form').validate({
		consignee: {
			rule: {
				required	:true,
				regexp		:/^[a-zA-Z]{1,20}$|^[\u4e00-\u9fa5]{1,10}$/
			},
			error: {
				required	:'收货人必填',
				regexp		: "格式不正确"
			},
			message: "请填写真实姓名,确保能送货成功",
			success: "ok!"
		},
		address:{
			//指定规则
			rule:{
				required:true,
				regexp 	:/^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]+/
			},
			//错误规则
			error:{
				required:'收货地址必填',
				regexp 	:'格式不正确',
			},
			//正常提示
			message:'请填入收货地址',
			//成功提示
			success:'ok!'
		},
		zipcode:{
			//指定规则
			rule:{
				required:true,
				regexp:/^[1-9][0-9]{5}$/
			},
			//错误规则
			error:{
				required:'邮政编码必填',
				regexp:'格式不正确'
			},
			//正常提示
			message:'请填入收货地邮政编码',
			//成功提示
			success:'ok!'
		},
		phone_buyer:{
			//指定规则
			rule:{
				required:true,
				phone:true
			},
			//错误规则
			error:{
				required:'买家的电话是必填的',
				phone:'格式不正确'
			},
			//正常提示
			message:'请输入买家的手机号码',
			//成功提示
			success:'ok!'
		}
	});

});
$(document).ready(function(){
	//ajax提交新收货地址
	$('#new_address_form').submit(function() {
		var data = $(this).serialize();
		var show_address=$('#show_address');
		$.ajax({
			url 	: add_address_url,
			type 	: 'POST',
			dataType: 'json',
			data 	: data,
			success : function(data){
				if(!data.state){
					alert(data.message);
					return false;
				}else{
					close_address();
					show_address.show();
					$('#submitbox').show();
					$('#addressbox').hide();
					show_address.find('.other_address_info').html(data.address);
					$('input[name=oconsignee]').val(data.oconsignee);
					$('input[name=oaddress]').val(data.oaddress);
					$('input[name=ozipcode]').val(data.ozipcode);
					$('input[name=ophone_cnee]').val(data.ophone_cnee);
					$('input[name=ophone_buyer]').val(data.ophone_buyer);
					return true;
				}
			}
		})
		
		// if ($(this).is_validate()) {
			
		// } else {
		// 	return false;
		// }
	});
});